package com.demo.config;


import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.*;
import org.springframework.jdbc.datasource.DriverManagerDataSource;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.transaction.annotation.EnableTransactionManagement;

import javax.sql.DataSource;

/**
 * @ClassName BeanConfig
 * @Description TODO
 * @Author Windy
 * @Date 2022/9/1 17:09
 * @Version 1.0
 **/

@Configuration
@PropertySource("classpath:jdbc.properties")
@ComponentScan(basePackages = "com.demo")
@Import(TransactionManagerConfig.class)
@EnableTransactionManagement
public class BeanConfig {
    @Value(value = "${jdbc.driver}")
    private String driver;
    @Value(value = "${jdbc.url}")
    private String url;
    @Value(value = "${jdbc.username}")
    private String username;
    @Value(value = "${jdbc.password}")
    private String password;

    /**
     * 获取数据库链接的数据源
     * @return 数据源
     */
    @Bean("dataSource")
    public DataSource dataSource() {
        System.out.println(driver);
        DriverManagerDataSource dataSource = new DriverManagerDataSource();
        System.out.println(driver);
        dataSource.setDriverClassName(driver);
        dataSource.setUrl(url);
        dataSource.setUsername(username);
        dataSource.setPassword(password);
        return dataSource;
    }


}
